package com.ruoyi.hanyun.controller;

import java.util.List;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.hanyun.domain.HyProjects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hanyun.domain.HyTasks;
import com.ruoyi.hanyun.service.IHyTasksService;

/**
 * 任务Controller
 */
@Controller
@RequestMapping("/hanyun/task")
public class HyTasksController extends BaseController {
    private String prefix = "hanyun/task";

    @Autowired
    private IHyTasksService hyTaskService;

    @PreAuthorize("@ss.hasPermi('hanyun:task:view')")
    @GetMapping()
    public String task() {
        return prefix + "/task";
    }

    /**
     * 查询任务列表
     */
    @PreAuthorize("@ss.hasPermi('hanyun:task:list')")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HyTasks hyTask) {
        startPage();
        List<HyTasks> list = hyTaskService.selectHyTaskList(hyTask);
        return this.getDataTable(list);
    }

    /**
     * 新增保存任务
     */
    @PreAuthorize("@ss.hasPermi('hanyun:task:add')")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody HyTasks hyTask) {
        return toAjax(hyTaskService.insertHyTask(hyTask));
    }

    /**
     * 修改任务
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        HyTasks hyTask = hyTaskService.selectHyTaskById(id);
        mmap.put("hyTask", hyTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存任务
     */
    @PreAuthorize("@ss.hasPermi('hanyun:task:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HyTasks hyTask) {
        return toAjax(hyTaskService.updateHyTask(hyTask));
    }

    /**
     * 删除任务
     */
    @PreAuthorize("@ss.hasPermi('hanyun:task:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hyTaskService.deleteHyTaskByIds(Convert.toLongArray(ids)));
    }

    /**
     * 获取子任务列表
     */
    @GetMapping("/children/{parentId}")
    @ResponseBody
    public List<HyTasks> children(@PathVariable("parentId") Long parentId) {
        return hyTaskService.selectHyTaskChildren(parentId);
    }

    /**
     * 更新任务状态
     */
    @PostMapping("/status/{taskId}/{status}")
    @ResponseBody
    public AjaxResult updateStatus(@PathVariable("taskId") Long taskId, @PathVariable("status") String status) {
        return toAjax(hyTaskService.updateTaskStatus(taskId, status));
    }

    /**
     * 更新任务进度
     */
    @PostMapping("/progress/{taskId}/{progress}")
    @ResponseBody
    public AjaxResult updateProgress(@PathVariable("taskId") Long taskId, @PathVariable("progress") Integer progress) {
        return toAjax(hyTaskService.updateTaskProgress(taskId, progress));
    }

    /**
     * 分配任务
     */
    @PostMapping("/assign/{taskId}/{ownerId}")
    @ResponseBody
    public AjaxResult assignTask(@PathVariable("taskId") Long taskId, @PathVariable("ownerId") Long ownerId) {
        return toAjax(hyTaskService.assignTask(taskId, ownerId, getUserId()));
    }

    /**
     * 添加任务依赖
     */
    @PostMapping("/dependency/{taskId}/{dependsOnTaskId}")
    @ResponseBody
    public AjaxResult addDependency(@PathVariable("taskId") Long taskId, @PathVariable("dependsOnTaskId") Long dependsOnTaskId) {
        return toAjax(hyTaskService.addTaskDependency(taskId, dependsOnTaskId));
    }

    /**
     * 移除任务依赖
     */
    @PostMapping("/dependency/remove/{taskId}/{dependsOnTaskId}")
    @ResponseBody
    public AjaxResult removeDependency(@PathVariable("taskId") Long taskId, @PathVariable("dependsOnTaskId") Long dependsOnTaskId) {
        return toAjax(hyTaskService.removeTaskDependency(taskId, dependsOnTaskId));
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
} 